package com.doudou.base.resources.controller;

import com.doudou.base.resources.entity.dto.user.details.UserDetailsInfoAddDto;
import com.doudou.base.resources.entity.dto.user.details.UserDetailsInfoUpdateDto;
import com.doudou.base.resources.service.UserDetailsInfoService;
import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.entity.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户的详细信息  前端控制器
 * @author 傻男人
 * @date 2020-05-14
 */
@Slf4j
@RestController
@RequestMapping("/base/user")
@Api(value = "用户的详细信息信息",tags = "用户的详细信息信息")
public class UserDetailsInfoController {

    @Autowired
    protected UserDetailsInfoService userDetailsInfoService;

    /**
     * 添加用户的详细信息
     * @param userDetailsInfoAddDto
     * @param result
     * @return
     */
    @ApiOperation(value = "添加用户的详细信息",notes = "添加用户的详细信息")
    @PostMapping(value = "/details")
    public Result addUserDetailsInfo(@Valid @RequestBody @ApiParam(value = "用户的详细信息添加dto",required = true) UserDetailsInfoAddDto userDetailsInfoAddDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        UserDetailsInfoDto userDetailsInfoDto = userDetailsInfoService.addUserDetailsInfo(userDetailsInfoAddDto);
        return Result.ok(userDetailsInfoDto);
    }

    /**
     * 根据ids删除用户的详细信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids删除用户的详细信息",notes = "根据ids删除用户的详细信息")
    @ApiImplicitParam(name = "ids",value="用户的详细信息ids",dataType="String",required = true,paramType = "path")
    @DeleteMapping(value = "/details/{ids}")
    public Result deleteUserDetailsInfo(@PathVariable("ids") String ids) {
        Integer number = userDetailsInfoService.deleteUserDetailsInfo(ids);
        return Result.ok(number);
    }

    /**
     * 根据id修改用户的详细信息
     * @param id
     * @param userDetailsInfoUpdateDto
     * @param result
     * @return
     */
    @ApiOperation(value = "根据id修改用户的详细信息",notes = "修改用户的详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户的详细信息id",dataType="Long",required = true,paramType = "path")})
    @PutMapping(value = "/details/{id}")
    public Result updateUserDetailsInfo(@PathVariable("id")  Long id,@Valid @RequestBody @ApiParam(value = "用户的详细信息修改dto",required = true) UserDetailsInfoUpdateDto userDetailsInfoUpdateDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        userDetailsInfoUpdateDto.setId(id);
        Integer sum = userDetailsInfoService.updateUserDetailsInfo(userDetailsInfoUpdateDto);
        return Result.ok(sum);
    }

    /**
     * 根据ids查询用户的详细信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids查询用户的详细信息",notes = "根据ids查询用户的详细信息")
    @ApiImplicitParam(name = "ids",value="用户的详细信息ids",dataType="String",required = true,paramType = "path")
    @GetMapping(value = "/details/{ids}")
    public Result findUserDetailsInfo(@PathVariable("ids")  String ids) {
        List<UserDetailsInfoDto> list = userDetailsInfoService.findUserDetailsInfo(ids);
        return Result.ok(list);
    }

    /**
    * 根据id查询用户的详细信息
    * @param primaryId   用户的详细信息的id
    * @return
    */
    @ApiOperation(value = "根据id查询用户的详细信息",notes = "根据id查询用户的详细信息")
    @ApiImplicitParam(name = "primaryId",value="用户的详细信息的id",dataType="Long",required = true,paramType = "path")
    @GetMapping(value = "/details/{primaryId}/primaryId")
    public Result findUserDetailsInfo(@PathVariable("primaryId")Long primaryId) {
        UserDetailsInfoDto result = userDetailsInfoService.findUserDetailsInfoById(primaryId);
        return Result.ok(result);
    }

}

